/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.TexturePaint;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.Timer;
import javax.swing.UIManager;

import com.ruipengkj.ApplicationConfig;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.factory.ImageFactory;
//import com.sun.awt.AWTUtilities;
/**
 * 说明：
 * 启动界面
 * @Author：李智深
 * @Date：2012-03-15
 */
public class InitMain extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;

	private JProgressBar bar;
	
	private Timer timer;
	
	private TexturePaint paint = Util.createTexturePaint(ImageFactory.loadImage(ImageFactory.START_BG).getImage());
	private Image backgroundLeftImage = ImageFactory.loadImage(ImageFactory.START_LEFT_BG).getImage();
	private Image backgroundRightImage = ImageFactory.loadImage(ImageFactory.START_RIGHT_BG).getImage();
	private Image backgroundlogo = ImageFactory.loadImage(ImageFactory.START_LOGO_BG).getImage();
	
	private int width=380;
	private int height=213;
	
//	private static final String ServiceURL = "http://www.dlwoo.com";
	
	public static void main(String[] args) {

		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
//		 java.awt.EventQueue.invokeLater(new Runnable() {
//			 public void run() {
//
//				    //更新文件版本标识URL
//				    String versionUrl = ServiceURL+"/"+"Version?verFlag=fastFood";
//
//				    URL url = null;
//				    InputStream is = null;
//				    InputStreamReader isr = null;
//				    BufferedReader netVer = null;
//
//				    //读取网络上的版本
//				    try {
//				    	 url = new URL(versionUrl);
//				         is = url.openStream();
//				         isr = new InputStreamReader(is);
//
//				         netVer = new BufferedReader(isr);
//				         double netVerStr = 0;
//				         double newVer = 0;
//				         Properties ps = InitContext.getinstance().LoadDataBaseData();
////				         String verStr = getNowVer();
//				         String verStr = ps.getProperty("pool.LocalVer");
//				         if(verStr==null || verStr.equals("")){
//				        	 verStr="0";
//				         }
//				         double localVerStr = Double.parseDouble(verStr);
//
//				         String str = "";
//
//				         while((str = netVer.readLine()) != null)
//				         {
//				         	String[] strs = str.split(",");
//				         	netVerStr = Double.parseDouble(strs[0]);
//				         	if(netVerStr>newVer){
//				         		newVer = netVerStr;
//				         	}
//				         }
//
//
//				         if (newVer>localVerStr) {
////				        	 CheckUpdate checkupdate = new CheckUpdate();
////			        		 checkupdate.setVisible(true);
//				        	 Runtime run=Runtime.getRuntime();
//				        	 run.exec("update.exe");
//				        	 //run.exec("E:\\Program Files\\店联网进销存\\update.exe", null, new File("E:\\Program Files\\店联网进销存"));
////				        	 run.exec("java -cp E:/资料/RPIntoSavePinSystem_FastFood/bin com.ruipengkj.bar.CheckUpdate");
////				        	 this.doShutDownWork();
//				        	 System.exit(0);
//				         }
//
//				    } catch (MalformedURLException ex) {
////				    	ex.printStackTrace();
////				    	JOptionPane
////						.showMessageDialog(null, "服务器地址错误！");
////				    	RPLogin rp = new RPLogin();
////						 rp.setVisible(true);
//				    } catch (IOException ex) {
////				    	ex.printStackTrace();
////				    	JOptionPane
////						.showMessageDialog(null, "未连接服务器！");
////				    	RPLogin rp = new RPLogin();
////						 rp.setVisible(true);
//				    } finally {
//				        //释放资源
//				        try {
//				        	if(netVer!=null){
//				        		netVer.close();
//				        	}
//				        	if(isr!=null){
//				        		isr.close();
//				        	}if(is!=null){
//				        		is.close();
//				        	}
//				        } catch (IOException ex1) {
////				        	ex1.printStackTrace();
//				        }
//				    }
//
//
//			 }
//
//			 });
		
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				InitMain initMain = new InitMain();
				initMain.setVisible(true);
			}
		});
	}

	public InitMain() {
		this.setUndecorated(true);
//		AWTUtilities.setWindowOpaque(this, false);
		this.setResizable(false);
		this.setSize(width, height);
		this.setLocationRelativeTo(null);
		this.setTitle(ApplicationConfig.APP_TITLE);
		this.setIconImage(ApplicationConfig.APP_IMAGE);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.init();
	}

	private void init() {
		JPanel main_jp=new JPanel(null){
			private static final long serialVersionUID = 1L;

			@Override
			protected void paintComponent(Graphics g) {
				Graphics2D g2d = (Graphics2D) g;
				//鼠标滑过
				g2d.drawImage(backgroundLeftImage, 0, 0, null);
				g2d.setPaint(paint);
				int x = backgroundLeftImage.getWidth(null);
				int y = 0;
				int width = getWidth() - x - backgroundRightImage.getWidth(null);
				int height = getHeight();
				g2d.fillRect(x, y, width, height);
				g2d.drawImage(backgroundRightImage, x + width, 0, null);
				g2d.drawImage(backgroundlogo, (getWidth()-backgroundlogo.getWidth(null))/2, 58, null);
//				g2d.dispose();
//				super.paintComponent(g);
			}
		};
		main_jp.setOpaque(false);
		JLabel jlb=new JLabel("版权所有：店联网        版本号：V.123",JLabel.CENTER);
		jlb.setForeground(Color.WHITE);
		jlb.setBounds(0, height-40, width, 30);
		
		bar = new JProgressBar(0, 209);
		
		bar.setBounds((width-200)/2, height-80, width-200, 20);
		bar.setBorder(BorderFactory.createLineBorder(new Color(95,162,207), 2));
		
		main_jp.add(bar);
		main_jp.add(jlb);
		
		this.add(main_jp);
		timer = new Timer(10, this);
		timer.start();
		new Thread(comm).start();
		
		ApplicationConfig.INIT_PROGRESS=3;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		bar.setValue(ApplicationConfig.INIT_PROGRESS);
	}

	private Runnable comm = new Runnable() {
		@Override
		public void run() {
			RPLogin login = new RPLogin();
			dispose();
			login.setVisible(true);
		}
	};
}
